package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author ：w
 * @date ：Created in 2019/7/30 0030
 * @description ：菜单控制器
 * @version: 1.0
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class menuController {
    @Reference
    private MenuService menuService;

    /**
     * 分页获取菜单数据
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findMenuListByPage")
    public PageResult findMenuListByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = null;
        try {
            pageResult = menuService.findMenuListByPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        } catch (Exception e) {
            e.printStackTrace();
            pageResult = new PageResult(0L,new ArrayList());
        }
        return pageResult;
    }

    /**
     * 新增菜单
     * @param map
     * @return
     */
    @RequestMapping("/addOneRecord4Menu")
    public Result addOneRecord4Menu(@RequestBody Map map) {
        try {
            menuService.addOneRecord4Menu(map);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 更新菜单
     * @param map
     * @return
     */
    @RequestMapping("/updateOneRecord4Menu")
    public Result updateOneRecord4Menu(@RequestBody Map map) {
        try {
            menuService.updateOneRecord4Menu(map);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 基于ID,获取菜单信息
     * @param id
     * @return
     */
    @RequestMapping("/findMenuById")
    public Result findMenuById(Integer id) {
        try {
            Menu menu = menuService.findMenuById(id);
            return new Result(true, MessageConst.ACTION_SUCCESS, menu);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
}
